/* This file is part of FireEscape.
 *
 * FireEscape is the intellectual work of Phillip Cheng & Allen Park.
 * All future releases or modifications of this program must include this
 * attribuation as according to the Creative Commons BY-SA license.
 *
 * FireEscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FireEscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FireEscape.  If not, see <http://www.gnu.org/licenses/>.
 *
 * All modifications to this program should include this original statement.
 *
 * If you modify, distribute, or use the program , please contact
 * fireescape@kloudvine.com
 * http://code.google.com/p/fireescape/
 *
 * If you are interested in using FireEscape for your structure, please contact us
 * for support.
 *
 * If you do use FireEscape, please consider donating to the project.
 */
package fireescape;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class compiles the data
 * @author drtechmaster
 */
public class DataCompiler {

    public static void main(String[] args) {
        Scanner userIn = new Scanner(System.in);
        ArrayList<String> hashTimeData = new ArrayList<String>();
        ArrayList<String> parentalInfo = new ArrayList<String>();
        ArrayList<String> scoresInfo = new ArrayList<String>();
        try {
            Scanner fileIn = new Scanner(new File(userIn.nextLine()));
            //create writers
            BufferedWriter clientList = new BufferedWriter(new FileWriter("C:\\clientList.txt"));
            BufferedWriter connections = new BufferedWriter(new FileWriter("C:\\connections.txt"));
            BufferedWriter genetics = new BufferedWriter(new FileWriter("C:\\genetics.txt"));
            BufferedWriter times = new BufferedWriter(new FileWriter("C:\\times.txt"));
            BufferedWriter scores = new BufferedWriter(new FileWriter("C:\\scores.txt"));
            while (fileIn.hasNext()) {
                String s = fileIn.nextLine();
                if (s.contains("Connected:")) {
                    clientList.newLine();
                    clientList.write(s);
                } else if (s.contains("processing client")) {
                    connections.newLine();
                    connections.write(s);
                } else if (s.contains("=")) {
                    genetics.newLine();
                    genetics.write(s);
                    parentalInfo.add(s);
                } else if (s.contains("@")) {
                    times.newLine();
                    times.write(s);
                    hashTimeData.add(s);
                } else if (s.contains("+")) {
                    scores.newLine();
                    scores.write(s);
                    scoresInfo.add(s);
                }

            }
            times.close();
            genetics.close();
            connections.close();
            clientList.close();
            scores.close();
        } catch (IOException ex) {
            Logger.getLogger(DataCompiler.class.getName()).log(Level.SEVERE, null, ex);

        }
        //change Strings into machine parsable strings
        for (int i = 0; i < scoresInfo.size(); i++) {
            String workingString = scoresInfo.get(i);
            workingString = workingString.replace("INFO: Score +1: ", "");
            scoresInfo.set(i, workingString);
        }
        for (int i = 0; i < parentalInfo.size(); i++) {
            String workingString = parentalInfo.get(i).replace("=", ";").replace("*", ";").replace("DATA: ", "");
            String[] temp = workingString.split(" : ");
            parentalInfo.set(i, temp[1]);
            //System.out.println(parentalInfo.get(i));
        }
        for (int i = 0; i < hashTimeData.size(); i++) {
            String workingString = hashTimeData.get(i).replace("@t: ", ";").replace("DATA: ", "");
            String[] temp = workingString.split(",");
            workingString = temp[0] + ";" + temp[1].split(";")[1];
            workingString = workingString.replace("gen: ", "");
            workingString = workingString.replace(" : ", ";");
            hashTimeData.set(i, workingString);
            //System.out.println(hashTimeData.get(i));
        }
        try {
            //write rankings
            BufferedWriter rankings = new BufferedWriter(new FileWriter("C:\\rankings.txt"));
            for (int i = 0; i < scoresInfo.size(); i++) {
                String s=scoresInfo.get(i);
                int instances=0;
                while (scoresInfo.remove(s)) {
                    instances++;
                }
                rankings.newLine();
                rankings.write(s+","+instances);
            }
            rankings.flush();
            rankings.close();
        } catch (IOException ex) {
            Logger.getLogger(DataCompiler.class.getName()).log(Level.SEVERE, null, ex);
        }
        //sort
        Collections.sort(hashTimeData, new HashTimeDataComparator());
        try {
            //write html referenced to pages about each
            BufferedWriter indexWriter = new BufferedWriter(new FileWriter("C:\\compiledData\\index.html"));
            //write html header
            indexWriter.newLine();
            indexWriter.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            indexWriter.newLine();
            indexWriter.write("<html>");
            indexWriter.newLine();
            indexWriter.write("<head>");
            indexWriter.newLine();
            indexWriter.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
            indexWriter.newLine();
            indexWriter.write("<title>" + "Index of Hashes" + "</title>");
            indexWriter.newLine();
            indexWriter.write("</head>");

            //write links
            for (String time : hashTimeData) {
                String splitTime[] = time.split(";");
                for (String s : parentalInfo) {
                    String array[] = s.split(";");
                    // System.out.println(array[0] + "-"+splitTime[0]);
                    // System.out.println(time);
                    //System.out.println(s);
                    if (array[0].equals(splitTime[1])) {
                        indexWriter.newLine();
                        //print out times
                        indexWriter.write("<p>gen: "+splitTime[0]+" time: " + splitTime[2] + " | ");
                        //label as anchor and provide links to png
                        indexWriter.write("<a name=\"" + array[0] + "\">" + array[0] + "</a>"
                                + "<a href=\"" + array[0] + "-0" + ".png\">" + " f0" + "</a>"
                                + "<a href=\"" + array[0] + "-1" + ".png\">" + " f1" + "</a>"
                                + "<a href=\"" + array[0] + "-2" + ".png\">" + " f2" + "</a>"
                                + " = ");

                        //print out mother and father anchors/images
                        indexWriter.write("<a href=\"#" + array[1] + "\">" + array[1] + "</a>"
                                + "<a href=\"" + array[1] + "-0" + ".png\">" + " f0" + "</a>"
                                + "<a href=\"" + array[1] + "-1" + ".png\">" + " f1" + "</a>"
                                + "<a href=\"" + array[1] + "-2" + ".png\">" + " f2" + "</a>" + " x ");
                        indexWriter.write("<a href=\"#" + array[2] + "\">" + array[2] + "</a>"
                                + "<a href=\"" + array[2] + "-0" + ".png\">" + " f0" + "</a>"
                                + "<a href=\"" + array[1] + "-1" + ".png\">" + " f1" + "</a>"
                                + "<a href=\"" + array[1] + "-2" + ".png\">" + " f2" + "</a></p>");
                    }
                }
            }
            indexWriter.flush();
            indexWriter.close();

        } catch (IOException ex) {
            Logger.getLogger(DataCompiler.class.getName()).log(Level.SEVERE, null, ex);
        }

        //write html for each page and generation <- could take forever
    }
}
